//
//  AuthenViewController.m
//  HxgProject
//
//  Created by hxg on 2018/3/1.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "AuthenViewController.h"
#import "AuthenTableViewCell.h"

#import "PersonInfoAuthenVC.h"
#import "ContectInfoVC.h"
#import "ReputationInfoVC.h"

#import "AuthenService.h"
#import "ContactInfo.h"
#import "ReputationDetail.h"
#import "PersonInfo.h"

@interface AuthenViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *autherTableView;

// 认证信息
@property (nonatomic, strong) NSMutableArray *authersArr;


// 图片信息
@property (nonatomic, strong) NSMutableArray *imgsArr;

// 认证描述信息
@property (nonatomic, strong) NSMutableArray *autherDesArr;

// 认证对象
@property (nonatomic, strong) AuthenService *authenService;

// 存储个人信息的全局变量
@property (nonatomic, strong) PersonInfo *globalPerson;

// 存储个人联系人信息
@property (nonatomic, strong) ContactInfo *globalContact;

// 存储个人信誉信息
@property (nonatomic, strong) ReputationDetail *globalXinYu;


@end

@implementation AuthenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
  @{NSFontAttributeName:[UIFont systemFontOfSize:19.0f],
    NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"]}];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self setUpUI];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 查询联系人认证
    [self getContactAuthenInfo];
    
    // 查询个人认证信息
    [self getPersonAuthenInfo];

    // 查询信誉认证
    [self getXinYuInfo];
}

#pragma mark - ###getActions###
- (void)getPersonAuthenInfo
{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getUserInfoWithCallBack:^(PersonInfo *person) {
        if (!isEmptyString(person.name)
            && !isEmptyString(person.idCard)
            && !isEmptyString(person.school)
            && !isEmptyString(person.schoolAddr))
        {
            weakSelf.globalPerson = person;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kPersonInfoAuthenRes];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kPersonInfoAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
    }];
}

- (void)getXinYuInfo
{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getReputationDetailWithCallBack:^(ReputationDetail *rep) {
        if (!isEmptyString(rep.jdAccount)
            && !isEmptyString(rep.alipayAccount)
            )
        {
            weakSelf.globalXinYu = rep;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kXinYuAuthenRes];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kXinYuAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
        
    }];
}

- (void)getContactAuthenInfo
{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getContactsWithCallBack:^(ContactInfo *info) {
        if (!isEmptyString(info.contactName1)
            && !isEmptyString(info.contactName2))
        {
            weakSelf.globalContact = info;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kContactAuthenRes];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kContactAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
    }];    
}

#pragma mark  ###UI###
- (void)setUpUI
{
    self.navigationItem.title = @"认证";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:68.0f/255.0f green:161.0f/255.0f blue:247.0f/255.0f alpha:1.0f];
    
    [self.view addSubview:self.autherTableView];
    [self.autherTableView registerClass:[AuthenTableViewCell class]
                 forCellReuseIdentifier:@"cell"];
}

-(UITableView *)autherTableView
{
    if (!_autherTableView)
    {
        _autherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreen_Width, kScreen_Height) style:UITableViewStylePlain];
        _autherTableView.delegate = self;
        _autherTableView.dataSource = self;
        _autherTableView.tableFooterView = [[UIView alloc] init];
    }
    
    return _autherTableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableViewDelegate
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    
    AuthenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell)
    {
        cell = [[AuthenTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.desLbl.text = self.autherDesArr[indexPath.row];
    cell.personIcon.image = [UIImage imageNamed:self.imgsArr[indexPath.row]];
    cell.titleNameLbl.text = [self.authersArr objectAtIndex:indexPath.row];
    
    if (0 == indexPath.row)
    {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:kPersonInfoAuthenRes])
        {
            cell.authenLbl.text = @"已认证";
            cell.authenLbl.textColor = [UIColor blueColor];
        }
        else
        {
            cell.authenLbl.text = @"未认证";
            cell.authenLbl.textColor = [UIColor redColor];
        }
    }
    else if (1 == indexPath.row)
    {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:kContactAuthenRes])
        {
            cell.authenLbl.text = @"已认证";
            cell.authenLbl.textColor = [UIColor blueColor];
        }
        else
        {
            cell.authenLbl.text = @"未认证";
            cell.authenLbl.textColor = [UIColor redColor];
        }
    }
    else if (2 == indexPath.row)
    {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:kXinYuAuthenRes])
        {
            cell.authenLbl.text = @"已认证";
            cell.authenLbl.textColor = [UIColor blueColor];
        }
        else
        {
            cell.authenLbl.text = @"未认证";
            cell.authenLbl.textColor = [UIColor redColor];
        }
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.authersArr.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        PersonInfoAuthenVC *personVC = [[PersonInfoAuthenVC alloc] init];
        personVC.showPerson = self.globalPerson;
        [self.navigationController pushViewController:personVC animated:YES];
    }
    else if (indexPath.row == 1)
    {
        ContectInfoVC *personVC = [[ContectInfoVC alloc] init];
        personVC.showContact = self.globalContact;
        
        [self.navigationController pushViewController:personVC animated:YES];
    }
    else
    {
        ReputationInfoVC *personVC = [[ReputationInfoVC alloc] init];
        personVC.showReputationInfo = self.globalXinYu;
        [self.navigationController pushViewController:personVC animated:YES];
    }
}

#pragma mark getter、setter

-(NSMutableArray *)authersArr
{
    if (!_authersArr) {
        _authersArr = [NSMutableArray arrayWithObjects:@"个人信息认证",@"联系人认证",@"信誉认证", nil];
    }
    return _authersArr;
}

- (NSMutableArray *)imgsArr
{
    if (!_imgsArr)
    {
        _imgsArr = [NSMutableArray arrayWithObjects:@"persionInfo",@"contact",@"authen", nil];
    }
    return _imgsArr;
}

- (NSMutableArray *)autherDesArr
{
    if (!_autherDesArr) {
        _autherDesArr = [NSMutableArray arrayWithObjects:@"请如实填写个人信息",@"请允许访问你的同学录",@"请填写平台真实信息", nil];
    }
    return _autherDesArr;
}

- (AuthenService *)authenService
{
    if (!_authenService)
    {
        _authenService = [[AuthenService alloc] init];
    }
    return _authenService;
}

-(PersonInfo *)globalPerson
{
    if (!_globalPerson) {
        _globalPerson = [[PersonInfo alloc] init];
    }
    return _globalPerson;
}

- (ContactInfo *)globalContact
{
    if (!_globalContact)
    {
        _globalContact = [[ContactInfo alloc] init];
    }
    return _globalContact;
}

- (ReputationDetail *)globalXinYu
{
    if (!_globalXinYu)
    {
        _globalXinYu = [[ReputationDetail alloc] init];
    }
    return _globalXinYu;
}

@end
